package org.hefan.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.exception.BusinessException;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.user.domain.User;
import org.hefan.user.dto.UserDto;
import org.hefan.user.query.UserQuery;
import org.hefan.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    /**
     * 注入userService
     */
    @Autowired
    private IUserService userService;

    /**
     * 查询全部
     * //处理请求：Get http://localhost:8080/user
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部用户",notes = "")
    public List<User> findAll() {
        return userService.loadAll();
    }

    /**
     * 查询单个User
     * 处理请求：Get http://localhost:8080/user/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "需要传入id")
    public User findOne(@PathVariable("id")Long id) {
        return userService.loadById(id);
    }

    /**
     * 分页查询
     * post请求,参数在请求体中,通过@RequestBody获取请求体中的参数
     * @param userQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询用户",notes = "需要传入分页数据")
    public PageList<User> queryPage(@RequestBody UserQuery userQuery) {
        return userService.queryPage(userQuery);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            userService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改用户
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改用户",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody User user) {
        try {
            if (user.getId() == null) {
                userService.save(user);
            } else {
                userService.update(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除用户",notes = "传需要批量删除的用户id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            userService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PostMapping("/register/phone")
    @ApiOperation(value = "用户通过手机注册接口",notes = "")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto) {
        try {
            userService.phoneRegister(userDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("注册失败!!!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误!!!" + e.getMessage());
        }
    }
}
